package zyy.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import zyy.util.BusinessException;
import zyy.util.BaseException;
import zyy.util.Bus;

public class FrmUserReg extends JDialog implements ActionListener {
  private static final long serialVersionUID = -5204426900618153440L;

  // 准备 区块儿 Panel
  private JPanel textPane = new JPanel();
  private JPanel infoPane = new JPanel();
  private JPanel toolPane = new JPanel();
  private JPanel sexPane = new JPanel();
  // 信息标签 (JLabel)
  private JLabel loginNameLabel = new JLabel("登录账户：");
  private JLabel yourNameLabel = new JLabel("你的名字");
  private JLabel PasswdLabel1 = new JLabel("密码： ");
  private JLabel PasswdLabel2 = new JLabel("请重复密码：");
  // private JLabel uSexLabel 性别还是用 单选框的BtuGroup吧
  private JLabel uTelLabel = new JLabel("电话号码：");
  private JLabel uEmailabel = new JLabel("电子邮箱：");
  private JLabel uCitylabel = new JLabel("所在城市：");
  // 接受输入 文本框(JTextField) | 密码框(JPasswordField) | 单选框(JRadioButton)
  private JTextField loginNameEdt = new JTextField(20);
  private JTextField yourNameEdt = new JTextField(20);
  private JPasswordField uPasswdEdt1 = new JPasswordField(20);
  private JPasswordField uPasswdEdt2 = new JPasswordField(18);
  private JTextField uTelEdt = new JTextField(20);
  private JTextField uEmaiEdt = new JTextField(20);
  private JTextField uCityEdt = new JTextField(20);
  private JRadioButton jrMale = new JRadioButton("男");
  private JRadioButton jrFemale = new JRadioButton("女");
  // 提交 | 取消 按钮(Button)
  private Button btnCommit = new Button("提交");
  private Button btnCancel = new Button("取消");

  private String regType = "骑手";

  public FrmUserReg(Dialog f, String s, boolean b, String Type) {
    super(f, s, b);
    {
      // 按钮和账户都是一样的
      toolPane.setLayout(new FlowLayout(FlowLayout.CENTER));
      toolPane.add(btnCancel);
      toolPane.add(btnCommit);
      this.btnCancel.addActionListener(this);
      this.btnCommit.addActionListener(this);
      this.getContentPane().add(toolPane, BorderLayout.SOUTH);

      infoPane.setLayout(new BorderLayout());
      this.getContentPane().add(infoPane, BorderLayout.CENTER);

      textPane.setLayout(new FlowLayout(FlowLayout.LEFT));
      textPane.add(loginNameLabel);
      textPane.add(loginNameEdt);
      textPane.add(yourNameLabel);
      textPane.add(yourNameEdt);
      infoPane.add(textPane, BorderLayout.CENTER);

    }
    this.regType = Type;
    switch (Type) {
      case "用户":
        textPane.add(PasswdLabel1);
        textPane.add(uPasswdEdt1);
        textPane.add(PasswdLabel2);
        textPane.add(uPasswdEdt2);
        textPane.add(uTelLabel);
        textPane.add(uTelEdt);
        textPane.add(uEmailabel);
        textPane.add(uEmaiEdt);
        textPane.add(uCitylabel);
        textPane.add(uCityEdt);

        ButtonGroup sexG = new ButtonGroup();
        sexG.add(jrFemale);
        sexG.add(jrMale);
        sexPane.setLayout(new FlowLayout(FlowLayout.CENTER));
        sexPane.add(jrFemale);
        sexPane.add(jrMale);
        infoPane.add(sexPane, BorderLayout.SOUTH);

        this.setTitle("用户注册");
        this.setSize(340, 285);

        break;
      case "骑手":
        // 骑手的注册框暂时只需要登录名和昵称
        this.setTitle("骑手注册");
        this.setSize(270, 170);
        break;
      case "管理员":
        // 管理员只需要多提供一份密码
        textPane.remove(loginNameLabel);
        textPane.remove(loginNameEdt);
        textPane.add(PasswdLabel1);
        textPane.add(uPasswdEdt1);
        textPane.add(PasswdLabel2);
        textPane.add(uPasswdEdt2);
        this.setTitle("管理员注册");
        this.setSize(330, 195);
        break;
    }
    {
      // 屏幕居中显示
      double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
      double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
      this.setLocation((int) (width - this.getWidth()) / 2, (int) (height - this.getHeight()) / 2);

      // 自适应调整组件大小
      this.validate();
    }
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == btnCommit) {
      switch (this.regType) {
        case "用户":
          String reglName = this.loginNameEdt.getText();
          String reguName = this.yourNameEdt.getText();
          String regPasswd1 = String.valueOf(this.uPasswdEdt1.getPassword());
          String regPasswd2 = String.valueOf(this.uPasswdEdt2.getPassword());
          String regTel = this.uTelEdt.getText();
          String regEmail = this.uEmaiEdt.getText();
          String regCity = this.uCityEdt.getText();
          int regSex = 0;
          if (jrMale.isSelected()) {
            regSex = 1;
          }
          try {
            System.out.println(
                "用户注册：" + reglName + reguName + regPasswd1 + regPasswd2 + regTel + regEmail + regCity + regSex);
            Bus.userManager.reg(reglName, reguName, regPasswd1, regPasswd2, regTel, regEmail, regCity, regSex);
            JOptionPane.showMessageDialog(null, String.format("用户 %s:%s 注册成功", reglName, reguName), "注册成功",
                JOptionPane.INFORMATION_MESSAGE);
            this.setVisible(false);
          } catch (BusinessException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
          }
          break;
        case "骑手":
          reglName = this.loginNameEdt.getText();
          reguName = this.yourNameEdt.getText();
          System.out.println("骑手注册 " + reglName + " " + reguName);
          try {
            Bus.riderManager.reg(reglName, reglName);
            this.setVisible(false);
            System.out.println(String.format("骑手 %s:%s 注册成功", reglName, reguName));
            // JOptionPane.showMessageDialog(null, String.format("骑手 %s:%s 注册成功", reglName,
            // reguName), "注册成功",
            // JOptionPane.INFORMATION_MESSAGE);
          } catch (BusinessException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
          }
          break;
        case "管理员":
          // reglName = this.loginNameEdt.getText();
          reguName = this.yourNameEdt.getText();
          regPasswd1 = String.valueOf(this.uPasswdEdt1.getPassword());
          regPasswd2 = String.valueOf(this.uPasswdEdt2.getPassword());
          try {
            Bus.adminManager.reg(reguName, regPasswd1, regPasswd2);
            this.setVisible(false);
          } catch (BaseException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
          }
          break;
        default:
          JOptionPane.showMessageDialog(null,
              String.format("请正确选择注册类型\nYour Type %s is not in [用户，骑手，管理员]", this.regType), "错误",
              JOptionPane.ERROR_MESSAGE);
          break;
      }
    } else if (e.getSource() == btnCancel) {
      this.setVisible(false);
    }
  }

  public static void main(String[] args) {
    FrmUserReg dlg0 = new FrmUserReg(null, "用户注册", true, "用户");
    FrmUserReg dlg1 = new FrmUserReg(null, "骑手注册", true, "骑手");
    FrmUserReg dlg2 = new FrmUserReg(null, "管理员注册", true, "管理员");
    dlg0.setVisible(true);
    dlg1.setVisible(true);
    dlg2.setVisible(true);
  }
}
